//
//  OrderExtHeaderView.m
//  HAODE
//
//  Created by panquanReabam.com on 16/5/17.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "OrderExtHeaderView.h"
#import "GoodsView.h"

@interface OrderExtHeaderView()
@end

@implementation OrderExtHeaderView

{
  NSMutableArray*sumPrices;
  GoodsView*shangpingView;

}
@synthesize mDelegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
     
        _headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 330)];
        [self addSubview:_headerView];
        UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 80/2)];
        view1.backgroundColor=[UIColor whiteColor];
        [_headerView addSubview:view1];
        UIView*gayView=[[UIView alloc]initWithFrame:CGRectMake(0, view1.bottom, ScreenWidth, 10)];
        gayView.backgroundColor=zuiqianhuiColor;
        gayView.layer.borderWidth = 0.35;
        gayView.layer.borderColor = qianhuiColor.CGColor;
        [_headerView addSubview:gayView];
        UILabel* duogou=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 40, 40)];
        duogou.text=[NSString stringWithFormat:@"员工"];
        duogou.textColor=zhonghuiColor;
        duogou.font=[UIFont systemFontOfSize:contenfot];
        [view1 addSubview:duogou];
        NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
        NSString* UserName = [user1 objectForKey:@"userName"];
        UIButton*userNameBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-200,  0,180,40)];
        [userNameBtn setTitle:UserName forState:UIControlStateNormal];
        userNameBtn.titleLabel.font=[UIFont systemFontOfSize:contenfot];
        userNameBtn.titleLabel.textAlignment=NSTextAlignmentRight;
        userNameBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        //[daogouname addTarget:self action:@selector(xuanzhidaogou:) forControlEvents:UIControlEventTouchUpInside];
        [userNameBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [view1 addSubview:userNameBtn];

    }
    return self;
}

-(void)setModel:(OrderExtHeaderModel *)model{
    
    if ([model.memberId isEqualToString:@""]) {
        _memberView=[[UIView alloc]initWithFrame:CGRectMake(0, 40+10, ScreenWidth, 70)];
        _memberView.backgroundColor=[UIColor whiteColor];
        [_headerView addSubview:_memberView];
        UIView*gayView1=[[UIView alloc]initWithFrame:CGRectMake(0, _memberView.bottom, ScreenWidth, 10)];
        gayView1.backgroundColor=zuiqianhuiColor;
        gayView1.layer.borderWidth = 0.35;
        gayView1.layer.borderColor = qianhuiColor.CGColor;
        [_headerView addSubview:gayView1];
        
        UILabel*memberLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, ScreenWidth-30, 40)];
        memberLabel.text=@"会员信息...";
        memberLabel.textColor=zhonghuiColor;
        memberLabel.center=_memberView.center;
        memberLabel.numberOfLines=2;
        memberLabel.font=[UIFont systemFontOfSize:contenfot];
        [_headerView addSubview:memberLabel];
        
        
        UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-80,18,0.35, 35)];
        l.backgroundColor=qianhuiColor;
        [_memberView addSubview:l];
        
        UIButton*chooseBtn = [[UIButton alloc] initWithFrame:CGRectMake( ScreenWidth-65, 23, 45,25)];
        [chooseBtn setTitle:@"选择" forState:UIControlStateNormal];
        [chooseBtn setTitleColor:shenhuiColor forState:UIControlStateNormal];
        [chooseBtn.layer setCornerRadius:1.0];
        chooseBtn.titleLabel.font=[UIFont systemFontOfSize:14];
        [chooseBtn.layer setBorderWidth:0.3];
        [chooseBtn.layer setMasksToBounds:YES];
        CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
        CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 0, 0, 0, 0.3 });
        [chooseBtn.layer setBorderColor:colorref2];//边框颜色
        [chooseBtn addTarget:self action:@selector(chooseBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_memberView addSubview:chooseBtn];
        
        UILabel*goods=[[UILabel alloc]initWithFrame:CGRectMake(15, _memberView.bottom+10, 80, 45)];
        goods.text=@"商品信息";
        goods.textColor=[UIColor blackColor];
        goods.font=[UIFont systemFontOfSize:titleSectionfot];
        [_headerView addSubview:goods];

    }else{

        _memberView=[[UIView alloc]initWithFrame:CGRectMake(0, 40+10, ScreenWidth,  250/2)];
        _memberView.backgroundColor=[UIColor whiteColor];
        [_headerView addSubview:_memberView];
        UIView*gayView1=[[UIView alloc]initWithFrame:CGRectMake(0, _memberView.bottom, ScreenWidth, 10)];
        gayView1.backgroundColor=zuiqianhuiColor;
        gayView1.layer.borderWidth = 0.35;
        gayView1.layer.borderColor = qianhuiColor.CGColor;
        [_headerView addSubview:gayView1];
        UILabel*goods=[[UILabel alloc]initWithFrame:CGRectMake(15, _memberView.bottom+10, 80, 45)];
        goods.text=@"商品信息";
        goods.textColor=[UIColor blackColor];
        goods.font=[UIFont systemFontOfSize:titleSectionfot];
        [_headerView addSubview:goods];


        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(ScreenWidth-55-10-13-13-8,50/2-5,8, 13);
        [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
        [jiantou addTarget:self action:@selector(chooseBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_memberView addSubview:jiantou];
        NSString *s = [NSString stringWithFormat:@"%@(%@)",   self.userName, self.phone];
        UIFont *font = [UIFont systemFontOfSize:16];
        CGSize size = CGSizeMake(320, 2000);
        CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
        UILabel*memberLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, labelsize.width+3, 50)];
        memberLabel.text=[NSString stringWithFormat:@"%@(%@)",   self.userName, self.phone];
        memberLabel.textColor=[UIColor blackColor];
        memberLabel.font=[UIFont systemFontOfSize:16];
        [_memberView addSubview:memberLabel];
        
        UIButton*suobtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 10, ScreenWidth-70,60)];
        [suobtn addTarget:self action:@selector(chooseBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_memberView addSubview:suobtn];
        
        UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-55-10-13,13,0.35, 30)];
        l.backgroundColor=qianhuiColor;
        [_memberView addSubview:l];
        UIButton*sxBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-55-10, 50/2-10, 55,25)];
        [sxBtn setTitle:@"修改" forState:UIControlStateNormal];
        [sxBtn setTitleColor:shenhuiColor forState:UIControlStateNormal];
        [sxBtn.layer setCornerRadius:1.0];
        sxBtn.titleLabel.font=[UIFont systemFontOfSize:14];
        [sxBtn.layer setBorderWidth:0.3];
        [sxBtn.layer setMasksToBounds:YES];
        CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
        CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 0, 0, 0, 0.3 });
        [sxBtn.layer setBorderColor:colorref2];//边框颜色
        [sxBtn addTarget:self action:@selector(chooseBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_memberView addSubview:sxBtn];
        
        
        
        UIImageView*Seximage=[[UIImageView alloc]initWithFrame:CGRectMake(memberLabel.right+19, 14, 20, 20)];
        if (![self.sex isEqual:[NSNull null]]) {
            
            if ([self.sex isEqualToString:@"男"]) {
                
                
                Seximage.image=[UIImage imageNamed:@"nan"];
                
            }else{
                
                Seximage.image=[UIImage imageNamed:@"nv"];
                
            }
            
            [_memberView addSubview:Seximage];
            
        }
        
        
        UIImageView*_xianvi = [[UIImageView alloc]initWithFrame:CGRectMake(15,50, ScreenWidth-30, 0.5)];
        _xianvi.image=[UIImage imageNamed:@"hengxian"];
        [_memberView addSubview:_xianvi];
        
        UILabel*consigneeNameLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,  _xianvi.bottom+12.5, 45, contenfot)];
        consigneeNameLabel.text=@"收货人";
        consigneeNameLabel.font= [UIFont systemFontOfSize:contenfot];
        [consigneeNameLabel setTextColor:zhonghuiColor];
        [_memberView addSubview:consigneeNameLabel];
        
        NSString *s121 =self.consigneeName;
        UIFont *font121 = [UIFont systemFontOfSize:contenfot];
        CGSize size121 = CGSizeMake(ScreenWidth-60, 2000);
        CGSize labelsize121 = [s121 sizeWithFont:font121 constrainedToSize:size121 lineBreakMode:NSLineBreakByCharWrapping];
        UILabel*daogou1=[[UILabel alloc]initWithFrame:CGRectMake(consigneeNameLabel.right, _xianvi.bottom+12.5,labelsize121.width, contenfot)];
        daogou1.text=self.consigneeName;
        daogou1.font= [UIFont systemFontOfSize:contenfot];
        [daogou1 setTextColor:shenhuiColor];
        [_memberView addSubview:daogou1];
        
        UILabel*mendian=[[UILabel alloc]initWithFrame:CGRectMake(daogou1.right+18, _xianvi.bottom+12.5, 30, contenfot)];
        mendian.text=@"电话";
        mendian.font= [UIFont systemFontOfSize:contenfot];
        [mendian setTextColor:zhonghuiColor];
        [_memberView addSubview:mendian];
        UILabel*mendian1=[[UILabel alloc]initWithFrame:CGRectMake(mendian.right,_xianvi.bottom+12.5, 200, contenfot)];
        mendian1.text=self.consigneePhone;
        mendian1.font= [UIFont systemFontOfSize:contenfot];
        [mendian1 setTextColor:shenhuiColor];
        [_memberView addSubview:mendian1];
        
        UIButton*jiantou1 = [[UIButton alloc]init];
        jiantou1.frame = CGRectMake(ScreenWidth-30,80,8, 13);
        [jiantou1 setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
        [jiantou1 addTarget:self action:@selector(inforemationBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_memberView addSubview:jiantou1];
        UIButton*jiada1=[[UIButton alloc]initWithFrame:CGRectMake(150, 60, ScreenWidth,60)];
        [jiada1 addTarget:self action:@selector(inforemationBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_memberView addSubview:jiada1];
     
        
        NSString *s1 =[NSString stringWithFormat:@"收货地址 %@",  self.address];
        UIFont *f1 = [UIFont systemFontOfSize:contenfot];
        CGSize sizedizhi = CGSizeMake(ScreenWidth-60, 2000);
        CGSize labelsizedizhi = [s1 sizeWithFont:f1 constrainedToSize:sizedizhi lineBreakMode:NSLineBreakByCharWrapping];
        
        
        UILabel*addressLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, consigneeNameLabel.bottom+5, ScreenWidth-60, labelsizedizhi.height)];
        addressLabel.text=[NSString stringWithFormat:@"收货地址 %@",  self.address];
        addressLabel.textColor=shenhuiColor;
        addressLabel.numberOfLines=2;
        addressLabel.font=[UIFont systemFontOfSize:contenfot];
        
        NSMutableAttributedString *AttributedSt = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"收货地址 %@",  self.address]];
        
        [AttributedSt addAttribute:NSFontAttributeName
         
                             value:[UIFont systemFontOfSize:contenfot]
         
                             range:NSMakeRange(0, 4)];
        
        [AttributedSt addAttribute:NSForegroundColorAttributeName
         
                             value:zhonghuiColor
         
                             range:NSMakeRange(0, 4)];
        
        
        addressLabel.attributedText = AttributedSt;
        
        [_memberView addSubview:addressLabel];

    }

    sumPrices =[[NSMutableArray alloc]init];
    for (int i = 0; i<self.dealPriceArray.count; i++) {
        double d;
        double d1;
        d =[self.dealPriceArray[i]doubleValue];
        d1 =[self.quantityArray[i]doubleValue];
        [sumPrices addObject:[NSString stringWithFormat:@"%f",d*d1]];
        
    }
    NSString*totalMoney= [sumPrices valueForKeyPath:@"@sum.floatValue"];
   NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:totalMoney.doubleValue]];
    
    
    NSNumber *sum11 = [self.quantityArray valueForKeyPath:@"@sum.floatValue"];
    _weightkeLabel=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-250, _memberView.bottom+10, 230, 45)];
    _weightkeLabel.text=[NSString  stringWithFormat:@"共 %@ 件   ￥%@",sum11, formattedNumberString];
    _weightkeLabel.textColor = [UIColor colorWithRed:5.00/255.0 green:18.0/255.0 blue:40.00/255.0 alpha:1];
    _weightkeLabel.textAlignment = NSTextAlignmentRight;
    _weightkeLabel.font = [UIFont systemFontOfSize:16];
    NSMutableAttributedString *AttributedStr2 = [[NSMutableAttributedString alloc]initWithString:[NSString  stringWithFormat:@"共 %@ 件   ￥%@",sum11, formattedNumberString]];
    [AttributedStr2 addAttribute:NSFontAttributeName
     
                           value:[UIFont systemFontOfSize:13.0]
     
                           range:NSMakeRange(0, 7)];
    
    [AttributedStr2 addAttribute:NSForegroundColorAttributeName
     
                           value:zhonghuiColor
     
                           range:NSMakeRange(0, 7)];
    
    
    _weightkeLabel.attributedText = AttributedStr2;
    
    
    
    
    [_headerView addSubview:_weightkeLabel];
    
    
    
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(15, _memberView.bottom+10+45-0.35, ScreenWidth-30, 0.35)];
    lineLabel.backgroundColor=qianhuiColor;
    [_headerView addSubview:lineLabel];
    
   
    if (self.headImageFullArray.count>0) {
        
        for (int i = 0; i<self.headImageFullArray.count; i++) {
            
            shangpingView = [[GoodsView alloc]initWithFrame:CGRectMake(0, lineLabel.bottom+(120*i), ScreenWidth, 120)];
            shangpingView.backgroundColor=[UIColor whiteColor];
            [shangpingView.headImageFull setImageWithURL:[self.headImageFullArray objectAtIndex:i]  placeholderImage:[UIImage imageNamed:@""]];
            [shangpingView.headImageFull.layer setCornerRadius:6.0];
            [shangpingView.headImageFull.layer setBorderWidth:0.3];
            [shangpingView.headImageFull.layer setMasksToBounds:YES];
            CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
            CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 0, 0, 0, 0.3 });
            [shangpingView.headImageFull.layer setBorderColor:colorref2];//边框颜色
            
            shangpingView.itemName.text=[NSString stringWithFormat:@"%@%@",[self.itemCodeArray objectAtIndex:i],[self.itemNameArray objectAtIndex:i]];
            
            shangpingView.quantity.text=[NSString stringWithFormat:@"数量 %@",[self.quantityArray objectAtIndex:i]];
            
            NSMutableAttributedString *AttributedStr1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"数量 %@",[self.quantityArray objectAtIndex:i]]];
            
            [AttributedStr1 addAttribute:NSFontAttributeName
             
                                   value:[UIFont systemFontOfSize:contenfot]
             
                                   range:NSMakeRange(0, 3)];
            
            [AttributedStr1 addAttribute:NSForegroundColorAttributeName
             
                                   value:zhonghuiColor
             
                                   range:NSMakeRange(0, 3)];
            
            
            shangpingView.quantity.attributedText = AttributedStr1;
            
            
            NSNumber*Total =[self.dealPriceArray objectAtIndex:i];
            
            NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
            [numberFormatter setPositiveFormat:@"###,##0.00;"];
            NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:Total.doubleValue]];
            shangpingView.dealPrice.text=[NSString stringWithFormat:@"¥ %@",formattedNumberString];
            shangpingView.dealPrice.textColor=hongseColor;
            shangpingView.itemType.text=[NSString stringWithFormat:@"%@",[self.specNameArray objectAtIndex:i]];
            
            NSMutableAttributedString *AttributedStr12 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ %@",formattedNumberString]];
            
            [AttributedStr12 addAttribute:NSFontAttributeName
             
                                    value:[UIFont systemFontOfSize:14]
             
                                    range:NSMakeRange(0, 1)];
            
            [AttributedStr12 addAttribute:NSForegroundColorAttributeName
             
                                    value:hongseColor
             
                                    range:NSMakeRange(0, 1)];
            
            
            shangpingView.dealPrice.attributedText = AttributedStr12;
    
            if (i==self.quantityArray.count-1) {
                UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 120-0.35, ScreenWidth, 0.35)];
                lineLabel.backgroundColor=qianhuiColor;
                [shangpingView addSubview:lineLabel];
            }else{
                UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(15, 120-0.35, ScreenWidth-30, 0.35)];
                lineLabel.backgroundColor=qianhuiColor;
                [shangpingView addSubview:lineLabel];
                }
            
            [_headerView addSubview:shangpingView];
            
        }
        
    }
    
    UIView*gayView1=[[UIView alloc]initWithFrame:CGRectMake(0, shangpingView.bottom, ScreenWidth, 10)];
    gayView1.backgroundColor=zuiqianhuiColor;
    gayView1.layer.borderWidth = 0.35;
    gayView1.layer.borderColor = qianhuiColor.CGColor;
    [_headerView addSubview:gayView1];

    
    UILabel* gatheringInformation=[[UILabel alloc]initWithFrame:CGRectMake(0, shangpingView.bottom+10, ScreenWidth, 45)];
    gatheringInformation.text=@"    收款信息";
    gatheringInformation.textColor=[UIColor blackColor];
    gatheringInformation.layer.borderColor = qianhuiColor.CGColor;
    gatheringInformation.backgroundColor=[UIColor whiteColor];
    gatheringInformation.font=[UIFont systemFontOfSize:15];
    [_headerView addSubview:gatheringInformation];
    _headerView.frame=CGRectMake(0, 0, ScreenWidth, gatheringInformation.bottom);


}

-(void)inforemationBtn:(UIButton*)sender{
    
    
    [mDelegate information:@""];
}


-(void)chooseBtn:(UIButton*)sender{


    [mDelegate chooesBtn:@""];
}

@end
